﻿using MediatR;
using SmartProduction.Domain;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.STaskCommand__生产管理;

namespace SmartProduction.Write.API.Application.CommandHandlers.STaskCommandHandler
{
    public class STaskDelCommandHandler : IRequestHandler<STaskDelCommand, APIResult<object>>
    {
        private readonly IBaseRepository<STask> sTaskRepository;

        public STaskDelCommandHandler(IBaseRepository<STask> sTaskRepository)
        {
            this.sTaskRepository = sTaskRepository;
        }
        public async Task<APIResult<object>> Handle(STaskDelCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var entity = sTaskRepository.GetAll().Result.FirstOrDefault(x => x.Tid == request.Tid);
            entity.IsDelete = 1;
            var i = sTaskRepository.UpdateAsync(entity).Result;
            if (i > 0)
            {
                result.Code = APIEnums.删除成功;
                result.Msg = APIEnums.删除成功.ToString();
                result.data = i;
            }
            else
            {
                result.Code = APIEnums.删除失败;
                result.Msg = APIEnums.删除失败.ToString();
                result.data = i;
            }
            return await Task.FromResult(result);
        }
    }
}
